/**
 * Created by chentao on 2015/8/13.
 */

var msg;
if (!msg) {
    msg = {}
}

$(function () {

    $("select[name=slt_group]").change(function () {
        searchData();
    });
    $("select[name=slt_type]").change(function () {
        searchData();
    });

    $("#btnsearch").click(function () {
        searchData();
    });

    $('input[name=ipt_sendtime]').on('apply.daterangepicker', function (ev, picker) {
        searchData();
    });

    $(".dropdown-menu li").click(function () {
        var txt = $(this).find("a").text();
        $(this).parent().parent().find("button").html(txt + "<span class='caret'></span>");
        $(this).addClass("active").siblings().removeClass("active");
    });

    initDateRange();

});

function initDateRange() {
    $(".rangedate").daterangepicker({
        "autoApply": true,
        "locale": {
            "applyLabel": "确定",
            "cancelLabel": "取消",
            "fromLabel": "开始",
            "toLabel": "结束",
            "customRangeLabel": "Custom",
            "weekLabel": "周",
            "daysOfWeek": [
                "日",
                "一",
                "二",
                "三",
                "四",
                "五",
                "六"
            ],
            "monthNames": [
                "1月",
                "2月",
                "3月",
                "4月",
                "5月",
                "6月",
                "7月",
                "8月",
                "9月",
                "10月",
                "11月",
                "12月"
            ],
            "cancelLabel": '清空',
            "opens": "right"
        },
        "format": "YYYY-MM-DD"
    }).on('cancel.daterangepicker', function (ev, picker) {
        $(this).val('');
    });
}

function searchData() {
    var group = $("select[name=slt_group]").val();
    var type = $("select[name=slt_type]").val();
    var content = $.trim($("input[name=ipt_content]").val());
    var sda = $("input[name=ipt_sendtime]").val();
    var searchFlg = $("#searchFlg").find(".active").data("value");
    var start = "", end = "";
    if (sda != null) {
        start = sda.split(" - ")[0];
        end = sda.split(" - ")[1];
    }

    var url = $().AbsUrlParse("/web/pushmsg/msg/listmsg?group=" + group
        + "&type=" + type + "&content=" + content + "&starttime=" + start + "&endtime=" + end + "&searchFlg=" + searchFlg);
    $("#table").bootstrapTable("refresh", {"url": url});

}

/**
 * 点击添加用户按钮
 */
bindClick("#addAdmin", function () {
    showModel("addAdmin-div", function () {
    });
});

//提交表单后回调
var saveAdmin = function (data) {
    var d = JSON.parse(data);
    console.log(d);
}

msg.editAdminShow = function (row, formid) {
    setRowToFrm(formid, row);
    return false;
}